package org.nanotek.hibernate.dao.impl;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.nanotek.base.Contact;
import org.nanotek.base.PageItemBase;
import org.nanotek.hibernate.dao.ContactsDAO;
import org.nanotek.hibernate.dao.GeneralPurposeDAO;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class ContactDAOImpl<T extends Contact> extends GeneralPurposeDAO<T> implements ContactsDAO<T>{

	public ContactDAOImpl() {
	}

	@Override
	@Transactional (readOnly = true, propagation = Propagation.SUPPORTS)
	public List<T> getContactsByName(String name) {
		PageItemBase<Long> pageItemBase = new PageItemBase<Long>(System.nanoTime());
		return getContactsByName(name , pageItemBase);
	}
	
	@Override
	@Transactional (readOnly = true, propagation = Propagation.SUPPORTS)
	public List<T> getContactsByName(String name , PageItemBase<?> pageItemBase) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(getClazz());
		detachedCriteria.add( Restrictions.like("name", name));
		return super.listRecords(pageItemBase.getStartRecord(), pageItemBase.getMaxRecords(), detachedCriteria);
	}

}
